type num = number;
class TextField extends egret.TextField { };
class Sprite extends egret.Sprite { };
class Shape extends egret.Shape { };
class DisplayObject extends egret.DisplayObject { };
class DisplayObjectContainer extends egret.DisplayObjectContainer { };
class Point extends egret.Point { };
class Rectangle extends egret.Rectangle { };
class Bitmap extends egret.Bitmap { };
class BitmapData extends egret.BitmapData { };
class Stage extends egret.Stage { };
class Tween extends egret.Tween { };
class Ease extends egret.Ease { };
/**颜色管理类 */
class XColor {
    static get random(): num { return Math.random() * 0XFFFFFF };


    /** 随机颜色数组 */
    static getRandomArray(count: num): num[] {
        let colors: num[] = [];
        for (let i = 0; i < count; i++) colors.push(XColor.random);
        return colors;
    }
    /** 设置颜色明暗, value值 [-255, 255] */
    static setColorBrightness(color: num, value: num): num {
        let r = (color >> 16) + value;
        if (r > 255) r = 255;
        else if (r < 0) r = 0;
        let b = ((color >> 8) & 0x00FF) + value;
        if (b > 255) b = 255;
        else if (b < 0) b = 0;
        let g = (color & 0x0000FF) + value;
        if (g > 255) g = 255;
        else if (g < 0) g = 0;

        return (g | (b << 8) | (r << 16));
    }

    /**  浅粉红 #FFB6C1 */
    static LightPink: num = 0xFFB6C1;
    /**  粉红 #FFC0CB */
    static Pink: num = 0xFFC0CB;
    /**  猩红 #DC143C */
    static Crimson: num = 0xDC143C;
    /**  脸红的淡紫色 #FFF0F5 */
    static LavenderBlush: num = 0xFFF0F5;
    /**  苍白的紫罗兰红色 #DB7093 */
    static PaleVioletRed: num = 0xDB7093;
    /**  热情的粉红 #FF69B4 */
    static HotPink: num = 0xFF69B4;
    /**  深粉色 #FF1493 */
    static DeepPink: num = 0xFF1493;
    /**  适中的紫罗兰红色 #C71585 */
    static MediumVioletRed: num = 0xC71585;
    /**  兰花的紫色 #DA70D6 */
    static Orchid: num = 0xDA70D6;
    /**  蓟 #D8BFD8 */
    static Thistle: num = 0xD8BFD8;
    /**  李子 #DDA0DD */
    static Plum: num = 0xDDA0DD;
    /**  紫罗兰 #EE82EE */
    static Violet: num = 0xEE82EE;
    /**  洋红 #FF00FF */
    static Magenta: num = 0xFF00FF;
    /**  灯笼海棠(紫红色) #FF00FF */
    static Fuchsia: num = 0xFF00FF;
    /**  深洋红色 #8B008B */
    static DarkMagenta: num = 0x8B008B;
    /**  紫色 #800080 */
    static Purple: num = 0x800080;
    /**  适中的兰花紫 #BA55D3 */
    static MediumOrchid: num = 0xBA55D3;
    /**  深紫罗兰色 #9400D3 */
    static DarkVoilet: num = 0x9400D3;
    /**  深兰花紫 #9932CC */
    static DarkOrchid: num = 0x9932CC;
    /**  靛青 #4B0082 */
    static Indigo: num = 0x4B0082;
    /**  深紫罗兰的蓝色 #8A2BE2 */
    static BlueViolet: num = 0x8A2BE2;
    /**  适中的紫色 #9370DB */
    static MediumPurple: num = 0x9370DB;
    /**  适中的板岩暗蓝灰色 #7B68EE */
    static MediumSlateBlue: num = 0x7B68EE;
    /**  板岩暗蓝灰色 #6A5ACD */
    static SlateBlue: num = 0x6A5ACD;
    /**  深岩暗蓝灰色 #483D8B */
    static DarkSlateBlue: num = 0x483D8B;
    /**  熏衣草花的淡紫色 #E6E6FA */
    static Lavender: num = 0xE6E6FA;
    /**  幽灵的白色 #F8F8FF */
    static GhostWhite: num = 0xF8F8FF;
    /**  纯蓝 #0000FF */
    static Blue: num = 0x0000FF;
    /**  适中的蓝色 #0000CD */
    static MediumBlue: num = 0x0000CD;
    /**  午夜的蓝色 #191970 */
    static MidnightBlue: num = 0x191970;
    /**  深蓝色 #00008B */
    static DarkBlue: num = 0x00008B;
    /**  海军蓝 #000080 */
    static Navy: num = 0x000080;
    /**  皇军蓝 #4169E1 */
    static RoyalBlue: num = 0x4169E1;
    /**  矢车菊的蓝色 #6495ED */
    static CornflowerBlue: num = 0x6495ED;
    /**  淡钢蓝 #B0C4DE */
    static LightSteelBlue: num = 0xB0C4DE;
    /**  浅石板灰 #778899 */
    static LightSlateGray: num = 0x778899;
    /**  石板灰 #708090 */
    static SlateGray: num = 0x708090;
    /**  道奇蓝 #1E90FF */
    static DoderBlue: num = 0x1E90FF;
    /**  爱丽丝蓝 #F0F8FF */
    static AliceBlue: num = 0xF0F8FF;
    /**  钢蓝 #4682B4 */
    static SteelBlue: num = 0x4682B4;
    /**  淡蓝色 #87CEFA */
    static LightSkyBlue: num = 0x87CEFA;
    /**  天蓝色 #87CEEB */
    static SkyBlue: num = 0x87CEEB;
    /**  深天蓝 #00BFFF */
    static DeepSkyBlue: num = 0x00BFFF;
    /**  淡蓝 #ADD8E6 */
    static LightBlue: num = 0xADD8E6;
    /**  火药蓝 #B0E0E6 */
    static PowDerBlue: num = 0xB0E0E6;
    /**  军校蓝 #5F9EA0 */
    static CadetBlue: num = 0x5F9EA0;
    /**  蔚蓝色 #F0FFFF */
    static Azure: num = 0xF0FFFF;
    /**  淡青色 #E1FFFF */
    static LightCyan: num = 0xE1FFFF;
    /**  苍白的绿宝石 #AFEEEE */
    static PaleTurquoise: num = 0xAFEEEE;
    /**  青色 #00FFFF */
    static Cyan: num = 0x00FFFF;
    /**  水绿色 #00FFFF */
    static Aqua: num = 0x00FFFF;
    /**  深绿宝石 #00CED1 */
    static DarkTurquoise: num = 0x00CED1;
    /**  深石板灰 #2F4F4F */
    static DarkSlateGray: num = 0x2F4F4F;
    /**  深青色 #008B8B */
    static DarkCyan: num = 0x008B8B;
    /**  水鸭色 #008080 */
    static Teal: num = 0x008080;
    /**  适中的绿宝石 #48D1CC */
    static MediumTurquoise: num = 0x48D1CC;
    /**  浅海洋绿 #20B2AA */
    static LightSeaGreen: num = 0x20B2AA;
    /**  绿宝石 #40E0D0 */
    static Turquoise: num = 0x40E0D0;
    /**  绿玉\碧绿色 #7FFFAA */
    static Auqamarin: num = 0x7FFFAA;
    /**  适中的碧绿色 #00FA9A */
    static MediumAquamarine: num = 0x00FA9A;
    /**  适中的春天的绿色 #F5FFFA */
    static MediumSpringGreen: num = 0xF5FFFA;
    /**  薄荷奶油 #00FF7F */
    static MintCream: num = 0x00FF7F;
    /**  春天的绿色 #3CB371 */
    static SpringGreen: num = 0x3CB371;
    /**  海洋绿 #2E8B57 */
    static SeaGreen: num = 0x2E8B57;
    /**  蜂蜜 #F0FFF0 */
    static Honeydew: num = 0xF0FFF0;
    /**  淡绿色 #90EE90 */
    static LightGreen: num = 0x90EE90;
    /**  苍白的绿色 #98FB98 */
    static PaleGreen: num = 0x98FB98;
    /**  深海洋绿 #8FBC8F */
    static DarkSeaGreen: num = 0x8FBC8F;
    /**  酸橙绿 #32CD32 */
    static LimeGreen: num = 0x32CD32;
    /**  酸橙色 #00FF00 */
    static Lime: num = 0x00FF00;
    /**  森林绿 #228B22 */
    static ForestGreen: num = 0x228B22;
    /**  纯绿 #008000 */
    static Green: num = 0x008000;
    /**  深绿色 #006400 */
    static DarkGreen: num = 0x006400;
    /**  查特酒绿 #7FFF00 */
    static Chartreuse: num = 0x7FFF00;
    /**  草坪绿 #7CFC00 */
    static LawnGreen: num = 0x7CFC00;
    /**  绿黄色 #ADFF2F */
    static GreenYellow: num = 0xADFF2F;
    /**  橄榄土褐色 #556B2F */
    static OliveDrab: num = 0x556B2F;
    /**  米色(浅褐色) #6B8E23 */
    static Beige: num = 0x6B8E23;
    /**  浅秋麒麟黄 #FAFAD2ow */
    static LightGoldenrodYell: num = 0xFAFAD2;
    /**  象牙 #FFFFF0 */
    static Ivory: num = 0xFFFFF0;
    /**  浅黄色 #FFFFE0 */
    static LightYellow: num = 0xFFFFE0;
    /**  纯黄 #FFFF00 */
    static Yellow: num = 0xFFFF00;
    /**  橄榄 #808000 */
    static Olive: num = 0x808000;
    /**  深卡其布 #BDB76B */
    static DarkKhaki: num = 0xBDB76B;
    /**  柠檬薄纱 #FFFACD */
    static LemonChiffon: num = 0xFFFACD;
    /**  灰秋麒麟 #EEE8AA */
    static PaleGodenrod: num = 0xEEE8AA;
    /**  卡其布 #F0E68C */
    static Khaki: num = 0xF0E68C;
    /**  金 #FFD700 */
    static Gold: num = 0xFFD700;
    /**  玉米色 #FFF8DC */
    static Cornislk: num = 0xFFF8DC;
    /**  秋麒麟 #DAA520 */
    static GoldEnrod: num = 0xDAA520;
    /**  花的白色 #FFFAF0 */
    static FloralWhite: num = 0xFFFAF0;
    /**  老饰带 #FDF5E6 */
    static OldLace: num = 0xFDF5E6;
    /**  小麦色 #F5DEB3 */
    static Wheat: num = 0xF5DEB3;
    /**  鹿皮鞋 #FFE4B5 */
    static Moccasin: num = 0xFFE4B5;
    /**  橙色 #FFA500 */
    static Orange: num = 0xFFA500;
    /**  番木瓜 #FFEFD5 */
    static PapayaWhip: num = 0xFFEFD5;
    /**  漂白的杏仁 #FFEBCD */
    static BlanchedAlmond: num = 0xFFEBCD;
    /**  Navajo白 #FFDEAD */
    static NavajoWhite: num = 0xFFDEAD;
    /**  古代的白色 #FAEBD7 */
    static AntiqueWhite: num = 0xFAEBD7;
    /**  晒黑 #D2B48C */
    static Tan: num = 0xD2B48C;
    /**  结实的树 #DEB887 */
    static BrulyWood: num = 0xDEB887;
    /**  (浓汤)乳脂,番茄等 #FFE4C4 */
    static Bisque: num = 0xFFE4C4;
    /**  深橙色 #FF8C00 */
    static DarkOrange: num = 0xFF8C00;
    /**  亚麻布 #FAF0E6 */
    static Linen: num = 0xFAF0E6;
    /**  秘鲁 #CD853F */
    static Peru: num = 0xCD853F;
    /**  桃色 #FFDAB9 */
    static PeachPuff: num = 0xFFDAB9;
    /**  沙棕色 #F4A460 */
    static SandyBrown: num = 0xF4A460;
    /**  巧克力 #D2691E */
    static Chocolate: num = 0xD2691E;
    /**  马鞍棕色 #8B4513 */
    static SaddleBrown: num = 0x8B4513;
    /**  海贝壳 #FFF5EE */
    static SeaShell: num = 0xFFF5EE;
    /**  黄土赭色 #A0522D */
    static Sienna: num = 0xA0522D;
    /**  浅鲜肉(鲑鱼)色 #FFA07A */
    static LightSalmon: num = 0xFFA07A;
    /**  珊瑚 #FF7F50 */
    static Coral: num = 0xFF7F50;
    /**  橙红色 #FF4500 */
    static OrangeRed: num = 0xFF4500;
    /**  深鲜肉(鲑鱼)色 #E9967A */
    static DarkSalmon: num = 0xE9967A;
    /**  番茄 #FF6347 */
    static Tomato: num = 0xFF6347;
    /**  薄雾玫瑰 #FFE4E1 */
    static MistyRose: num = 0xFFE4E1;
    /**  鲜肉(鲑鱼)色 #FA8072 */
    static Salmon: num = 0xFA8072;
    /**  雪 #FFFAFA */
    static Snow: num = 0xFFFAFA;
    /**  淡珊瑚色 #F08080 */
    static LightCoral: num = 0xF08080;
    /**  玫瑰棕色 #BC8F8F */
    static RosyBrown: num = 0xBC8F8F;
    /**  印度红 #CD5C5C */
    static IndianRed: num = 0xCD5C5C;
    /**  纯红 #FF0000 */
    static Red: num = 0xFF0000;
    /**  棕色 #A52A2A */
    static Brown: num = 0xA52A2A;
    /**  耐火砖 #B22222 */
    static FireBrick: num = 0xB22222;
    /**  深红色 #8B0000 */
    static DarkRed: num = 0x8B0000;
    /**  栗色 #800000 */
    static Maroon: num = 0x800000;
    /**  纯白 #FFFFFF */
    static White: num = 0xFFFFFF;
    /**  白烟 #F5F5F5 */
    static WhiteSmoke: num = 0xF5F5F5;
    /**  Gainsboro #DCDCDC */
    static Gainsboro: num = 0xDCDCDC;
    /**  浅灰色 #D3D3D3 */
    static LightGrey: num = 0xD3D3D3;
    /**  银白色 #C0C0C0 */
    static Silver: num = 0xC0C0C0;
    /**  深灰色 #A9A9A9 */
    static DarkGray: num = 0xA9A9A9;
    /**  灰色 #808080 */
    static Gray: num = 0x808080;
    /**  暗淡的灰色 #696969 */
    static DimGray: num = 0x696969;
    /**  纯黑 #000000 */
    static Black: num = 0x000000;

}

/** 文本标签(支持前缀后缀) */
class Label extends egret.TextField {
    private pre = '';
    private post = '';

    /**不带前后缀的文本 */
    str: string;
    /**
     * 
     * @param str 标签主体内容
     * @param pre 标签前缀
     * @param post 标签后缀
     */
    constructor(str: string, pre: string = "", post: string = "") {
        super();
        this.str = str;
        this.pre = pre;
        this.post = post;
        this.text = pre + str + post;

        this.textAlign = egret.HorizontalAlign.CENTER;
        this.verticalAlign = egret.VerticalAlign.MIDDLE;
        this.multiline = true;
    }
    /**Label标签的整数值 */
    get intVal(): num {
        return parseInt(this.str);
    }
    set intVal(val: num) {
        this.str = val.toString();
        this.text = this.pre + this.str + this.post;
    }

}

enum ButtonShape {
    SHP_ROUNDRECT,
    SHP_RECT,
    SHP_CIRCLE,

}
/** 按钮 */
class Button extends DisplayObjectContainer {
    static defWidth = 250;
    static defHeight = 60;
    static defColor = XColor.DeepSkyBlue;

    bg: DisplayObject;
    protected label: Label;
    protected bgColor: num;
    protected shape: ButtonShape;

    /**
     * 
     * @param text 标签文本
     * @param w 按钮宽度
     * @param h 按钮高度
     * @param c 按钮背景颜色
     */
    public constructor(text: string, w: num = Button.defWidth,
        h: num = Button.defHeight, c: num = Button.defColor) {

        super();

        this.width = w;
        this.height = h;
        this.bgColor = c;

        this.label = new Label(text);
        this.label.width = w;
        this.label.height = h;
        this.bg = XUI.RoundRect(w, h, c);

        this.shape = ButtonShape.SHP_ROUNDRECT;

        this.once(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
    }
    private addToStage(): void {
        this.enable();
        this.draw();
    }
    protected draw() {
        this.removeChildren();
        this.addChild(this.bg);
        this.addChild(this.label);

    }

    public enable(): void {
        this.disable();
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouch, this);
        this.setGray(false);
    }
    public disable(): void {
        this.touchEnabled = false;
        this.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouch, this);
        if (this.stage)
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouch, this);
        this.setGray(true);
    }
    /**
     * size [width,height] 设置按钮的尺寸
     */
    set size(size: num[]) {
        this.width = size[0] || Button.defWidth;
        this.height = size[1] || Button.defHeight;
        this.label.width = this.shape == ButtonShape.SHP_CIRCLE ? this.height : this.width;
        this.label.height = this.height;
        this.draw();
    }
    set textColor(value: num) {
        this.label.textColor = value;
    }
    /**按钮文本 */
    set text(value: string) {
        this.label.text = value;
    }
    get text(): string {
        return this.label.text;
    }
    /**
     * @summary 设置按钮样式
     * @param shp 按钮形状
     * @param color 颜色
     * @param isLineMode 是否仅绘制边框
     * @param w 宽度
     * @param h 高度(圆形按钮的直径)
     */
    setStyle(shp: ButtonShape, color: num = XColor.DeepSkyBlue, isLineMode: boolean = false, w: num = 250, h: num = 60) {
        switch (shp) {
            case ButtonShape.SHP_ROUNDRECT: {
                if (isLineMode) {
                    this.bg = XUI.LineRoundRect(w, h, color);
                } else {
                    this.bg = XUI.RoundRect(w, h, color);
                }
                break;
            }
            case ButtonShape.SHP_RECT: {
                if (isLineMode) {
                    this.bg = XUI.LineRect(w, h, color);
                } else {
                    this.bg = XUI.Rect(w, h, color);
                }
                break;
            }
            case ButtonShape.SHP_CIRCLE: {
                if (isLineMode) {
                    this.bg = XUI.LineCircle(h, color);
                    this.bg.x = this.bg.y = h / 2;
                } else {
                    this.bg = XUI.Circle(h, color);
                    this.bg.x = this.bg.y = h / 2;
                }
                break;
            }
        }
        this.shape = shp;
        this.size = [w, h];
        this.bgColor = color;
        this.draw();
    }

    protected onTouch(e: egret.TouchEvent): void {
        if (e.type == egret.TouchEvent.TOUCH_BEGIN) {
            this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouch, this);
            this.scaleX = this.scaleY = 0.95;
            this.x += 2;
            this.y += 2;
        } else {
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouch, this);
            this.scaleX = this.scaleY = 1;
            this.x -= 2;
            this.y -= 2;
        }
    }

    /**设置可示对象是否为灰色 */
    public setGray(isGray: boolean): void {
        if (isGray) {
            this.filters = [new egret.ColorMatrixFilter([
                0.3, 0.6, 0.08, 0, 0,
                0.3, 0.6, 0.08, 0, 0,
                0.3, 0.6, 0.08, 0, 0,
                0, 0, 0, 1, 0])];
        } else {
            this.filters = null;
        }
    }

}

/** 开关 */
class Switch extends Button {
    isEnable: boolean = true;
    constructor(enable: boolean = true) {
        super('');
        this.isEnable = enable;
        if (this.isEnable) {
            this.bg = XUI.Switch(XColor.SkyBlue, XColor.White, 1);
        } else {
            this.bg = XUI.Switch(XColor.Gray, XColor.White, 0);
        }
    }

    protected onTouch(e: egret.TouchEvent): void {
        if (e.type == egret.TouchEvent.TOUCH_BEGIN) {
            this.isEnable = !this.isEnable;
            if (this.isEnable) {
                this.bg = XUI.Switch(XColor.SkyBlue, XColor.White, 1);
            } else {
                this.bg = XUI.Switch(XColor.Gray, XColor.White, 0);
            }
            this.draw();
        }
    }

}

class ProgressBar extends DisplayObjectContainer {
    private bg: egret.Sprite;
    private fg: egret.Sprite;

    constructor(w: num = 1000, h: num = 50) {
        super();
        this.width = w;
        this.height = h;
        this.once(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    onAddToStage() {

        this.bg = XUI.LineRect(this.width, this.height, XColor.Gray);
        this.fg = XUI.Rect(1, this.height, this.color);

        this.addChild(this.bg);
        this.addChild(this.fg);
    }
    set width(w: num) {
        this.bg = XUI.LineRect(w, this.height, XColor.Gray);
    }
    get width() {
        return this.bg.width;
    }
    set value(val: number) {
        this.curVal = val; 
        let w = this.bg.width * (val-this.minVal) / (this.maxVal-this.minVal);
        this.removeChild(this.fg);
        this.fg = XUI.Rect(w, this.height, this.color);
        this.addChild(this.fg);
    }
    get value() {
        return this.curVal;
    }

    minVal = 0;
    maxVal = 100;
    curVal = 0;
    color = XColor.Purple;
}
/** 自动关闭的提示信息框 */
class Tip extends egret.Sprite {
    text: Label;
    closeTime: number;
    constructor(str: string, closeTime: number = 2000) {
        super();
        this.text = new Label(str);
        this.closeTime = closeTime;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {
        let bg = XUI.RoundRect(300, 60, XColor.Gray);
        this.addChild(bg);
        this.text.x = (300 - this.text.width) / 2;
        this.text.y = (60 - this.text.height) / 2
        this.addChild(this.text);

        this.x = (this.stage.stageWidth - this.width) / 2;
        this.y = (this.stage.stageHeight - this.height) / 2;


        Tween.get(this).to({ alpha: 0.9 }, 200).wait(200)
            .to({ alpha: 0 }, this.closeTime - 200).call(this.callback, this);
    }

    private callback(): void {
        Tween.removeTweens(this);
        if (this.parent)
            this.parent.removeChild(this);
    }
}




/** 
 * UI class
 */
class XUI {

    static TickMark(c: num = XColor.Green): Sprite {
        let s = new Sprite;
        let g = s.graphics;

        g.beginFill(0x333333, 0.7);
        g.lineStyle(2, 0x252525);
        g.drawCircle(0, 0, 20);
        g.endFill();
        g.lineStyle(4, c);
        g.moveTo(-10, -3);
        g.lineTo(-2, 8);
        g.lineTo(10, -8);

        return s;
    }

    static ErrorMark(c: num = XColor.Red): Sprite {
        let s = new Sprite;
        let g = s.graphics;

        g.beginFill(0x333333, 0.7);
        g.lineStyle(2, 0x252525);
        g.drawCircle(0, 0, 20);
        g.endFill();
        g.lineStyle(4, c);
        g.moveTo(-8, -8);
        g.lineTo(8, 8);
        g.moveTo(-8, 8);
        g.lineTo(8, -8);

        return s;
    }

    /**得到矩形框*/
    static LineRect(w: num, h: num, c: num = 0, t: num = 1, x: num = 0, y: num = 0): Sprite {
        let s: Sprite = new Sprite;
        s.graphics.lineStyle(t, c);
        s.graphics.drawRect(x, y, w, h);
        s.graphics.endFill();
        return s;
    }

    /**得到圆角矩形*/
    static RoundRect(w: num, h: num, c: num = 0, ew: num = 25, eh: num = 25, x: num = 0, y: num = 0): Sprite {
        let s: Sprite = new Sprite()
        s.graphics.beginFill(c);
        s.graphics.drawRoundRect(x, y, w, h, ew, eh);
        s.graphics.endFill();
        return s;
    }

    /**得到圆角矩形框*/
    static LineRoundRect(w: num, h: num, c: num = 0, ew: num = 25, eh: num = 25, x: num = 0, y: num = 0): Sprite {
        let s = new Sprite;
        s.graphics.lineStyle(1, c);
        s.graphics.drawRoundRect(x, y, w, h, ew, eh);
        return s;
    }

    /**得到圆形*/
    static Circle(r: num, c: num = 0, x: num = 0, y: num = 0): Sprite {
        let s: Sprite = new Sprite();
        s.graphics.beginFill(c);
        s.graphics.drawCircle(x, y, r);
        s.graphics.endFill();
        return s;
    }

    /**得到矩形-switchButton bc背景颜色，gc钩选的颜色,type为0是没有钩为1是有钩*/
    static Switch(bc: num = 0XFFFFFF, gc: num = 0, type: num = 0): Sprite {
        let node: Sprite = XUI.RoundRect(80, 50, bc, 60, 60);
        node.addChild(XUI.Circle(22, gc, type == 0 ? 25 : 55, 25));
        return node;
    }


    /**得到圆形框*/
    static LineCircle(r: num, c: num = 0, s: num = 1, x: num = 0, y: num = 0): Sprite {
        let node: Sprite = new Sprite();
        node.graphics.lineStyle(s, c)
        node.graphics.drawCircle(x, y, r);
        node.graphics.endFill();
        return node;
    }
    /**得到渐变矩形 a为角度偏移率0,0.5,1,2分别为四个正方向*/
    static MatrixRect(w: num, h: num, c1: num = 0, c2: num = 0, a: num = 0): Sprite {
        let node = new Sprite();
        let matrix = new egret.Matrix();
        matrix.createGradientBox(w, h, Math.PI * a, 0, 0);
        node.graphics.beginGradientFill(egret.GradientType.LINEAR, [c1, c2], [1, 1], [0, 255], matrix);
        node.graphics.drawRect(0, 0, w, h);
        node.graphics.endFill();
        return node;
    }
    /**得到矩形*/
    static Rect(w: num, h: num, c: num = 0, x: num = 0, y: num = 0): Sprite {
        let s: Sprite = new Sprite()
        s.graphics.beginFill(c);
        s.graphics.drawRect(x, y, w, h);
        s.graphics.endFill();
        return s;
    }
    /**得到矩形和一个X*/
    static RectAndX(w: num, h: num, c: num = 0, x: num = 0, y: num = 0): Sprite {
        let s: Sprite = this.Rect(w, h, c, x, y)
        s.addChild(this.CloseButton(w, h, c, 1, x, y));
        return s;
    }
    /**得到矩形和一个X*/
    static CloseButton(w: num, h: num, c: num = 0, s: num = 1, x: num = 0, y: num = 0): Sprite {
        let container: Sprite = new Sprite;
        let l1: Sprite = new Sprite;
        l1.graphics.lineStyle(s, c);
        l1.graphics.moveTo(0, 0);
        l1.graphics.lineTo(w, h);
        let l2: Sprite = new Sprite;
        l2.graphics.lineStyle(s, c);
        l2.graphics.moveTo(w, 0);
        l2.graphics.lineTo(0, h);
        container.addChild(l1);
        container.addChild(l2);
        return container;
    }

    /**得到多边形,side边数,rotation角度*/
    static Polygon(side: num = 3, r: num = 10, c: num = 0, rotation: num = 0): Sprite {
        let s: Sprite = new Sprite;
        s.rotation = rotation;
        s.graphics.beginFill(c);
        for (let i: num = 0; i <= side; i++) {
            let lineX: num = Math.cos((i * (360 / side) * Math.PI / 180)) * r;
            let lineY: num = Math.sin((i * (360 / side) * Math.PI / 180)) * r;
            if (i == 0) s.graphics.moveTo(lineX, lineY);
            else s.graphics.lineTo(lineX, lineY);

        }
        s.graphics.endFill();
        return s;
    }
    /**得到圆角矩形与三角形合体rc是正方形颜色,pc是三角形颜色*/
    static ArrowRoundRect(w: num, h: num, rc: num, pc: num = 0, rotation: num = 0): Sprite {
        let s: Sprite = new Sprite;
        s.addChild(this.RoundRect(w, h, rc));
        let p: Sprite = this.Polygon(3, w / 3, pc, 30 + rotation);
        p.x = s.width >> 1; p.y = s.height >> 1;
        s.addChild(p);
        return s;
    }
    /**得到滚动条的bar*/
    static ScrollLineBar(w: num, h: num, c: num): Sprite {
        let s: Sprite = new Sprite;
        let _h: num = h / 3;
        for (let i: num = 0; i < 3; i++) {
            let r: Sprite = this.Rect(w, 1, c, 0, i * _h);
            s.addChild(r);
        }
        return s;
    }
    /**得到圆角矩形-加*/
    static AddRoundRect(w: num, h: num, c: num): Sprite {
        let s: Sprite = new Sprite;
        s.addChild(this.RoundRect(w, h, c));
        let r1: Sprite = this.Rect(w / 2, 2, 0, w / 4, h / 2 - 1);
        let r2: Sprite = this.Rect(2, h / 2, 0, w / 2 - 1, h / 4);
        s.addChild(r1);
        s.addChild(r2);
        return s;
    }
    /**得到圆角矩形-减*/
    static RemoveRoundRect(w: num, h: num, c: num): Sprite {
        let s: Sprite = new Sprite;
        s.addChild(this.RoundRect(w, h, c));
        let r: Sprite = this.Rect(w / 2, 2, 0, w / 4, h / 2 - 1);
        s.addChild(r);
        return s;
    }
    /**得到带文字的圆角方形*/
    static RoundRectText(w: num, h: num, c: num, str: string = "click"): Sprite {
        let s: Sprite = new Sprite;
        s.addChild(this.RoundRect(w, h, c));
        let text: TextField = new TextField;
        text.name = "text";
        text.text = str;
        text.x = (s.width - text.width) >> 1;
        text.y = (s.height - text.height) >> 1;
        s.addChild(text);
        return s;
    }

    /**得到矩形-复选框 bc背景颜色，gc钩的颜色,type为0是没有钩为1是有钩*/
    static CheckBoxRect(bc: num = 0XFFFFFF, gc: num = 0, type: num = 0): Sprite {
        let s: Sprite = new Sprite;
        s.addChild(this.Rect(40, 40, bc));
        if (type == 1) {
            let r: Sprite = new Sprite;
            r.graphics.beginFill(gc);
            r.graphics.moveTo(0, 20);
            r.graphics.lineTo(20, 36); r.graphics.lineTo(44, 8); r.graphics.lineTo(36, 0); r.graphics.lineTo(20, 18);
            r.graphics.lineTo(12, 8); r.graphics.lineTo(0, 20);
            s.addChild(r);
        }
        return s;
    }
    /**得到矩形-单选框 bc背景颜色，gc钩的颜色,type为0是没有圆为1是有圆*/
    static RadioCircle(bc: num = 0XFFFFFF, gc: num = 0, type: num = 0): Sprite {
        let s: Sprite = new Sprite;
        s.addChild(this.Circle(16, bc, 16, 16));
        s.graphics.lineStyle(1, 0);
        if (type == 1) {
            let r: Sprite = this.Circle(8, gc, 16, 16)
            s.addChild(r);
        }
        return s;
    }
    /**得到矩形-网格
     * rect.x是x轴数量
     * rect.y是y轴数量
     * rect.width是网格宽
     * rect.height是网格高
     * lc网格线颜色
     * */
    static Gridding(rect: Rectangle, c: num = 0): Sprite {
        let s: Sprite = new Sprite;
        s.graphics.lineStyle(0.1, c);
        let disx: num = rect.width / rect.x;
        let disy: num = rect.height / rect.y;
        for (let i: num = 0; i < rect.x; i++) {
            s.graphics.moveTo(0, i * disy);
            s.graphics.lineTo(rect.width, i * disy);
        }
        for (let i = 0; i < rect.y; i++) {
            s.graphics.moveTo(i * disx, 0);
            s.graphics.lineTo(i * disx, rect.height);
        }
        return s;
    }
    /***得到爱心 */
    static Heart(r: num = 15, c: num = 0XFF0000): Sprite {
        let s: Sprite = new Sprite;
        s.graphics.beginFill(c);
        s.graphics.moveTo(0, 0);
        s.graphics.lineTo(0, -r * 2)
        s.graphics.cubicCurveTo(r, -r * 2.5, r * 2, -r * 1.5, 0, 0);
        s.graphics.moveTo(0, 0);
        s.graphics.lineTo(0, -r * 2)
        s.graphics.cubicCurveTo(-r, -r * 2.5, -r * 2, -r * 1.5, 0, 0);
        s.graphics.endFill();
        s.anchorOffsetX = -s.width / 2;
        s.anchorOffsetY = -s.height;
        return s;
    }
}

class Theme {
    static btnColorNormal = XColor.DeepSkyBlue;
    static btnColorDown = XColor.LightSteelBlue;
    static swithColorOff = XColor.Gray;

    /**默认按钮 */
    static get btnNormal() { return XUI.RoundRect(200, 60, Theme.btnColorNormal) };
    static get btnDown() { return XUI.RoundRect(200, 60, Theme.btnColorDown) };

    /**默认开关 */
    static get switchOn() { return XUI.Switch(Theme.btnColorNormal, XColor.White, 1) };
    static get switchOff() { return XUI.Switch(Theme.swithColorOff, XColor.White) };
};


class MoonEvent extends egret.EventDispatcher {
    //button event
    static readonly MOUSE_OVER: string = "event-over";//移进
    static readonly MOUSE_OUT: string = "event-out";//移出
    static readonly MOUSE_DOWN: string = "event-down";//点下
    static readonly MOUSE_MOVE: string = "event-move";//移动
    static readonly MOUSE_UP: string = "event-up";//弹开
    static readonly CLICK: string = "event-click";//单击

    //tabbar event
    static readonly CHANGE: string = "change";//更换
    static readonly COMPLETE: string = "complete";//完成
    static readonly ERROR: string = "error";//错误
    static readonly RENDER_COMPLETE: string = "render complete";//渲染完成
    static readonly UPDATE: string = "update";//更新
    static readonly START: string = "start";//开始
    static readonly MOVE: string = "move";//移动
    static readonly OVER: string = "over";//结束
    static readonly PAUSE: string = "pause";//暂停
    static readonly STOP: string = "stop";//停止
    static readonly PLAY: string = "play";//播放
    static readonly OPEN: string = "open";//开启
    static readonly CLOSE: string = "close";//关闭

    public currentTarget: Object;
    public type: string;
    public data: Object;
    public dataType: Object;
    public constructor(type: string = "", data: Object = null, currentTarget: Object = null) {
        super();
        this.type = type;
        this.data = data;
        this.currentTarget = currentTarget;
    }
}

class Container extends egret.DisplayObjectContainer {
    private dataEvent = new Object;
    protected stageWidth: num;
    protected stageHeight: num;

    public constructor() {
        super();
        this.init();
        this.once(egret.Event.ADDED_TO_STAGE, this.addToStage, this);
    }
    private addToStage(): void {
        this.render();
    }
    /** 加载到舞台之前调用 */
    protected init(): void {

    }
    /** 加载到舞台之后调用 */
    protected render(): void {
        this.stageWidth = this.stage.stageWidth;
        this.stageHeight = this.stage.stageHeight;
    }
    /** 创建渐变背景 */
    protected createBgGradientFill(c1: number = XColor.Blue, c2: number = XColor.BlueViolet): Sprite {
        let w: number = this.stageWidth;
        let h: number = this.stageHeight;
        let matrix: egret.Matrix = new egret.Matrix();
        matrix.createGradientBox(w * 2, h * 2, Math.PI / 2);
        let sprite: Sprite = new Sprite;
        sprite.graphics.beginGradientFill(egret.GradientType.RADIAL, [c1, c2], [1, 1], [0, 255], matrix);
        sprite.graphics.drawRect(0, 0, w, h);
        this.addChild(sprite);
        return sprite;
    }
    /** 发布事件*/
    public dispEvent(type: string, data: Object = null, dataType: Object = null): void {
        if (this.dataEvent) {
            let fun: Function = this.dataEvent[type] as Function;
            if (fun != null) {
                let moonEvent: MoonEvent = new MoonEvent;
                moonEvent.currentTarget = this;
                moonEvent.data = data;
                moonEvent.type = type;
                moonEvent.dataType = dataType;
                if (fun["this"]) {
                    (<Function>fun).apply(fun["this"], [moonEvent]);
                } else {
                    fun(moonEvent)
                }
            }
        }
    }
    /** 帧听事件*/
    public addEvent(type: string, listener: Function, thisObj: any = null): void {
        if (this.dataEvent && this.dataEvent[type] == null) {
            listener["this"] = thisObj
            this.dataEvent[type] = listener;
        }
    }
    /** 删除事件*/
    public removeEvent(type: string, listener: Function): void {
        if (this.dataEvent && this.dataEvent[type]) {
            delete this.dataEvent[type];
        }
    }
    /** 把自己从父级删除*/
    public removeFromParent(value: Boolean = false): void {
        let _parent = this.parent as DisplayObjectContainer;
        if (value) this.dispose();
        if (_parent && _parent.contains(this)) _parent.removeChild(this);
        _parent = null;
    }
    /** 删除所有子元素*/
    public removeChildren(dispose: Boolean = false): void {
        while (this.numChildren > 0) {
            this.removeChildIndex(0, dispose);
        }
    }
    /**删除index层的*/
    public removeChildIndex(index: num, dispose: Boolean): void {
        if (index >= 0 || index < this.numChildren) {
            let basicContent = this.getChildAt(index) as Container;
            if (basicContent instanceof Container) {
                basicContent.removeFromParent(dispose);
            } else {
                let display: DisplayObject = this.getChildAt(index) as DisplayObject;
                if (display.parent) display.parent.removeChild(display);
            }

        }
    }
    /**销毁*/
    public dispose(): void {
        this.removeChildren(true);
        this.dataEvent = null;
        this.stageWidth = null;
        this.stageHeight = null;
    }
}

class Layout {
    static placeCenter(parent: egret.DisplayObjectContainer, obj: egret.DisplayObject) {
        obj.x = (parent.stage.stageWidth - obj.width) / 2;
        obj.y = (parent.stage.stageHeight - obj.height) / 2;
        parent.addChild(obj);
    }
    static place(parent: egret.DisplayObjectContainer, obj: egret.DisplayObject, x: num, y: num) {
        obj.x = x;
        obj.y = y;
        parent.addChild(obj);
    }
}
/**
 * 模态对话框
 */
class Dialog extends Container {
    text: Label;
    bgColor = XColor.LightBlue;
    bgAlpha = 0.9;
    dlgBorderColor = XColor.White;
    dlgBgColor = XColor.Gray;
    dlgWidth = 400;
    dglHeight = 300;

    dlgBG: Sprite;

    constructor(str: string) {
        super();
        this.text = new Label(str);
    }
    /** 加载到舞台之前调用 */
    protected init(): void {

    }
    protected render(): void {
        this.touchEnabled = true;
        let bg = XUI.Rect(this.stage.stageWidth, this.stage.stageHeight, this.bgColor);
        bg.alpha = this.bgAlpha;
        this.addChild(bg);

        let line = XUI.LineRoundRect(this.dlgWidth, this.dglHeight, this.dlgBorderColor);
        Layout.placeCenter(this, line);
        let rr = XUI.RoundRect(this.dlgWidth, this.dglHeight, this.dlgBgColor);
        Layout.placeCenter(this, rr);
        this.dlgBG = rr;

        let label = this.text;
        label.width = 400;
        label.height = 200;
        rr.addChild(label);

        let btn = new Button('确定');
        btn.x = (rr.width - btn.width) / 2;
        btn.y = 220;
        rr.addChild(btn);
        btn.addEventListener(egret.TouchEvent.TOUCH_TAP, (e) => {
            this.parent.removeChild(this);
        }, this);
    }
}

